<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreScheduleRequest;
use App\Models\Project;
use App\Models\Schedule;
use App\Services\ScheduleService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ScheduleController extends Controller
{
    protected $ScheduleService;
    public function __construct(ScheduleService $ScheduleService)
    {
        $this->ScheduleService = $ScheduleService;
    }

    public function index(Request $Request) {
        $search = $Request->search;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        
        $sql = $this->ScheduleService->get($search);
        if(Auth::user()->company_id > 0){
            $sql->where('project_id',$Request->project_id);
        }
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('created_at','desc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data]);
    }

    public function show(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = Schedule::where('id',$Request->id)->first();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    /**
     * 存储一个新用户。
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreScheduleRequest $Request)
    {
        $POSTONLY = Schedule::POSTONLY;
        $input = $Request->only($POSTONLY);
        $data = $this->ScheduleService->insert($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function update(StoreScheduleRequest $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = Schedule::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['id'] = $Request->id;
        $data = $this->ScheduleService->update($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function delete(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = Schedule::find($Request->id);
        $data->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }

    public function statistics(Request $Request) {
        $res = $this->ScheduleService->statistics($Request->project_id);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$res['data'],'region'=>$res['region'],'title'=>$res['title'],'tomorrow'=>$res['tomorrow'],'day'=>$res['day'],'config'=>$res['config']]);
    }

    public function import(Request $Request) {
        if(!$Request->type_id){
            return response()->json(['code' => '404', 'msg' => '请选择类别']);
        }

        $data = $Request->data;
        if(!$data){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $Project = new Project();
        $user = $Project->getUsers($Request->project_id);
        if(!$user){
            return response()->json(['code' => '404', 'msg' => '请先添加项目人员']);
        }
        $data = json_decode($data);
        foreach ($data as $k => $v) {
            $data[$k]->project_id = $Request->project_id;
            $data[$k]->type_id = $Request->type_id;
            $data[$k]->person_id = isset($user[$v->name]) ? $user[$v->name] : 0;
            if($data[$k]->person_id == 0){
                return response()->json(['code' => '404', 'msg' => '责任人'.$v->name.'不存在']);
            }
            unset($v->name);
        }
        $data = $this->ScheduleService->importInsert($data);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$user]);
    }
}
